<script setup lang="ts">
import { useChartAutoResize } from '@/mixins/MixinChartAutoResize'
import type { VChartInstance } from '@/types'
import VueEcharts from 'vue-echarts'

const props = defineProps<{
  data: any[]
}>()

const designStore = useDesignStore()
const option = computed(() => {
  // echarts 柱状图option
  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
      },
    },
    legend: {
      show: false,
    },
    grid: {
      containLabel: true,
      left: 40,
      right: 50,
      top: 40,
      bottom: 30,
    },
    xAxis: {
      type: 'value',
    },
    yAxis: {
      type: 'category',
      data: props.data.map(item => item.name),
    },
    series: [
      {
        type: 'bar',
        data: props.data.map(item => item.value),
      },
    ],
  }
})

const chartRef = ref<VChartInstance>()

useChartAutoResize(chartRef)
</script>

<template>
  <VueEcharts
    ref="chartRef"
    :option="option" :init-options="{
      locale: 'zh',
    }" :theme="designStore.theme"
  />
</template>
